//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.lhrsite.jdbc;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import javax.sql.DataSource;
import net.sf.json.JSONArray;

public class Procedure {
    private DataSource dataSource;
    private List<Param> comeParameters;
    private String procedureName;
    private List<Param> outParameters;
    private JSONArray resultArray;
    private List resultList;
    private int paramsNumber;
    private String returnStr;
    private Boolean isReturn = Boolean.valueOf(false);
    private Connection conn;
    private CallableStatement cstm;

    /** @deprecated */
    @Deprecated
    public Procedure(String procedureName, List<Param> comeParameters, DataSource dataSource) {
        this.procedureName = procedureName;
        this.comeParameters = comeParameters;
        this.paramsNumber = comeParameters.size();
        this.dataSource = dataSource;
    }

    public Procedure(String procedureName, List<Param> comeParameters) {
        this.procedureName = procedureName;
        this.comeParameters = comeParameters;
        this.paramsNumber = comeParameters.size();
    }

    /** @deprecated */
    @Deprecated
    public Procedure(String procedureName, DataSource dataSource) {
        this.procedureName = procedureName;
        this.paramsNumber = 0;
        this.dataSource = dataSource;
    }

    public Procedure(String procedureName) {
        this.procedureName = procedureName;
        this.paramsNumber = 0;
    }

    /** @deprecated */
    @Deprecated
    public Procedure(String procedureName, List<Param> comeParameters, List<Param> outParameters, DataSource dataSource) {
        this.procedureName = procedureName;
        this.comeParameters = comeParameters;
        this.outParameters = outParameters;
        this.paramsNumber = comeParameters.size() + outParameters.size();
        this.dataSource = dataSource;
    }

    public Procedure(String procedureName, List<Param> comeParameters, List<Param> outParameters) {
        this.procedureName = procedureName;
        this.comeParameters = comeParameters;
        this.outParameters = outParameters;
        this.paramsNumber = comeParameters.size() + outParameters.size();
    }

    public void commit() throws SQLException {
        String baseSql = this.getBaseSql();
        if(this.dataSource == null) {
            this.dataSource = DataSourceUtil.getDataSource();
            if(this.dataSource == null) {
                throw new RuntimeException("未初始化DataSource.");
            }
        }

        this.conn = this.dataSource.getConnection();
        this.cstm = this.conn.prepareCall(baseSql);
        if(this.paramsNumber > 0) {
            this.cstm = this.setParam(this.cstm);
        }

        Boolean hashResult = Boolean.valueOf(false);

        try {
            hashResult = Boolean.valueOf(this.cstm.execute());
        } catch (SQLException var4) {
            throw var4;
        }

        this.getResult(hashResult);
        this.getOutParam();
        this.close();
    }

    private String getBaseSql() {
        String procedureModel = "{call " + this.procedureName + "(#{params})}";
        if(this.isReturn.booleanValue()) {
            procedureModel = procedureModel.replace("call", "? = call");
        }

        this.paramsNumber = 0;
        if(this.comeParameters != null) {
            this.paramsNumber += this.comeParameters.size();
        }

        if(this.outParameters != null) {
            this.paramsNumber += this.outParameters.size();
            if(this.isReturn.booleanValue()) {
                --this.paramsNumber;
            }
        }

        if(this.paramsNumber == 0) {
            return procedureModel.replace("#{params}", "");
        } else {
            String param = "";

            for(int i = 0; i < this.paramsNumber; ++i) {
                param = param + "?,";
            }

            if(param.length() >= 1) {
                param = param.substring(0, param.length() - 1);
            }

            return procedureModel.replace("#{params}", param);
        }
    }

    private CallableStatement setParam(CallableStatement cstm) throws SQLException {
        if(this.comeParameters != null) {
            String str = "";

            for(int i = 0; i < this.comeParameters.size(); ++i) {
                Param param = (Param)this.comeParameters.get(i);
                if(param.getData() == null) {
                    cstm.setString(param.getLocation(), (String)null);
                } else {
                    cstm.setString(param.getLocation(), param.getData().toString());
                }
            }
        }

        if(this.outParameters != null) {
            for(int i = 0; i < this.outParameters.size(); ++i) {
                Param param = (Param)this.outParameters.get(i);
                cstm.registerOutParameter(param.getLocation(), param.getType());
            }
        }

        return cstm;
    }

    public static List resultSetToList(ResultSet rs) throws SQLException {
        if(rs == null) {
            return Collections.EMPTY_LIST;
        } else {
            ResultSetMetaData md = rs.getMetaData();
            int columnCount = md.getColumnCount();
            List list = new ArrayList();
            HashMap rowData = null;

            while(rs.next()) {
                rowData = new HashMap(columnCount);

                for(int i = 1; i <= columnCount; ++i) {
                    rowData.put(md.getColumnName(i), rs.getObject(i));
                }

                list.add(rowData);
            }

            return list;
        }
    }

    public JSONArray getResultArray() {
        JSONArray js = null;
        if(this.resultList == null) {
            js = new JSONArray();
        } else {
            Gson gson = (new GsonBuilder()).setDateFormat("yyyy-MM-dd HH:mm:ss").create();
            js = JSONArray.fromObject(gson.toJson(this.resultList));
        }
        return js;
    }

    public void close() {
        try {
            if(this.cstm != null) {
                this.cstm.close();
                this.cstm = null;
            }

            if(this.conn != null) {
                this.conn.close();
                this.conn = null;
            }
        } catch (SQLException var2) {
            var2.printStackTrace();
        }

    }

    public void getResult(Boolean hashResult) throws SQLException {
        int i = 0;
        this.resultList = new ArrayList();
        while(true) {
            ++i;
            if(hashResult.booleanValue()) {
                ResultSet rs = this.cstm.getResultSet();
                List list = JdbcUtil.resultSetToList(rs);
                this.resultList.add(list);
                rs.close();
                rs = null;
            } else {
                int updateCount = this.cstm.getUpdateCount();
                if(updateCount == -1) {
                    return;
                }
            }
            hashResult = Boolean.valueOf(this.cstm.getMoreResults());
        }
    }
    public void getOutParam() throws SQLException {
        if(this.outParameters != null && this.outParameters.size() > 0) {
            for(int i = 0; i < this.outParameters.size(); ++i) {
                Param param = (Param)this.outParameters.get(i);
                param.setData(this.cstm.getObject(param.getLocation()));
                this.outParameters.set(i, param);
            }
        }
    }
}
